﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
#if !NETCOREAPP
using System.Web;
using System.Web.Mvc;
using System.Web.WebPages;
    using System.Web.Mvc.Html;

#endif

#if NETCOREAPP
using Microsoft.AspNetCore.Mvc.Rendering;
using Kugar.Core.Web;
#endif
using Kugar.Core.ExtMethod;
using Kugar.MVC.MobileUI.WeUI.Helpers;

namespace Kugar.MVC.MobileUI.WeUI.PageControls.PageForm
{
    /// <summary>
    /// 单图片字段,可点击上传,需返回ResultReturn类型的json数据,其中
    /// </summary>
    public class PageForm_SingleImage:PageForm_FieldBase
    {
        private bool _enableUpload = true;
        private string _src = "";
        private string _uploadScript = "";

        private string _postUrl = "";
        private string _width = "80px";
        private string _height = "80px";

        public PageForm_SingleImage(ViewContext viewContext) : base(viewContext)
        {
            this.Css("weui-cells_singleimage");
        }

        /// <summary>
        /// 默认图片框的高度
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height">如果高度为空,则自动等于宽度</param>
        /// <returns></returns>
        public PageForm_SingleImage ImageSize(string width, string height = "")
        {
            

            _width = width;
            _height = height;

            return this;
        }


        public PageForm_SingleImage DisableUpload(bool disableUpload = true)
        {
            _enableUpload = !disableUpload;

            return this;
        }

        public PageForm_SingleImage Src(string src)
        {
            _src = src.ToStringEx();

            return this;
        }

        /// <summary>
        /// 自定义上传图片的脚本代码,点击上传的时候,会调用该脚本,如果为空,则使用自定义的脚本
        /// </summary>
        /// <param name="uploadscript"></param>
        /// <returns></returns>
        public PageForm_SingleImage UploadScript(string uploadscript)
        {
            _uploadScript = uploadscript.ToStringEx();

            return this;
        }

        /// <summary>
        /// 使用异步的方式上传图片
        /// </summary>
        /// <param name="postUrl">图片文件提交的地址,需返回ResultReturn,ReturnData为{ImageUrl:"图片地址"} 的格式</param>
        /// <returns></returns>
        public PageForm_SingleImage Ajax(string postUrl)
        {
            _postUrl = postUrl.ToStringEx();

            return this;
        }

        protected override void BeginRender()
        {
            

            if (_enableUpload)
            {
                this.Data("data-enableupload", _enableUpload.ToStringEx());

                //if (_postUrl=="")
                //{
                //    throw new ArgumentOutOfRangeException("AjaxUploadUrl");
                //}
                if (!string.IsNullOrWhiteSpace(_postUrl))
                {
                    
                    this.Data("data-ajaxposturl", _postUrl);
                }
                
            }

            if (!string.IsNullOrWhiteSpace(_uploadScript))
            {
                this.Data("data-uploadscript", _uploadScript);
            }
        }

        //protected override void RenderHeader()
        //{
        //    //base.RenderHeader();
        //}

        //protected override void RenderBody()
        //{
        //    ViewContext.Writer.WriteLine($"<p>{getLabel()}</p>");
        //}

        protected override void RenderFooter()
        {
            if (_enableUpload)
            {
                ViewContext.Writer.WriteLine($"<input type=\"file\" name=\"{getName()}\" style=\"display:none;\" />");
                ViewContext.Writer.WriteLine($"<input type=\"hidden\" name=\"{getName()}\"  value=\"{_src}\" />");
            }

            if (string.IsNullOrWhiteSpace(_height))
            {
                _height = _width;
            }

            //if (!string.IsNullOrWhiteSpace(_width))
            //{
            //    this.Style("width", _width);
            //}

            //if (!string.IsNullOrWhiteSpace(_height))
            //{
            //    this.Style("height", _height);
            //}

            ViewContext.Writer.WriteLine($"<img class=\"weui_img\" style=\"{(string.IsNullOrWhiteSpace(_width)?"":$"width:{_width};")} {(string.IsNullOrWhiteSpace(_height) ? "" : $"height:{_height};")}\" src=\"{_src}\" alt=\"\" />");
        }
    }
}